package com.elves.gateway.common;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;

@Slf4j
public class OkHttpUtil {
    public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static String post(String url, String params) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static Response post(String url, Headers headers, String params) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .headers(headers)
                .build();
        Response response = client.newCall(request).execute();
        return response;
    }

    public static Response post(String url, String username, String password, String params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new BasicAuthInterceptor(username, password))
                .build();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        return client.newCall(request).execute();
    }

    public static Response post(String url, String username, String password, Headers headers, String params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new BasicAuthInterceptor(username, password))
                .build();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .headers(headers)
                .build();
        return client.newCall(request).execute();
    }

    /**
     * basic auth
     * 可以用
     * @param url
     * @param username
     * @param password
     * @param paramsMap
     * @return
     * @throws IOException
     */
    public static Response post(String url, String username, String password, Map<String, String> paramsMap) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new BasicAuthInterceptor(username, password))
                .build();
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        RequestBody formBody = builder.build();
        log.info("formBody:{}", formBody.toString());
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        return client.newCall(request).execute();
    }

    /**
     *
     * 可以用
     * @param url
     * @param paramsMap
     * @param key header的key
     * @param val header的val
     * @return
     * @throws IOException
     */
    public static Response post(String url, Map<String, String> paramsMap, String key, String val) throws IOException {
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        RequestBody formBody = builder.build();
        log.info("formBody:{}", formBody.toString());
        Request request = new Request.Builder()
                .url(url)
                .header(key,val)
                .post(formBody)
                .build();
        return client.newCall(request).execute();
    }


}
